package com.nsy.controller.user;

import com.github.pagehelper.PageInfo;
import com.nsy.entity.Result;
import com.nsy.pojo.Member;
import com.nsy.pojo.SysUser;
import com.nsy.service.IMemberService;
import com.nsy.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/member")
public class MemberController {
    @Autowired
    public IMemberService memberService;
    //跳转到登录页面
    @RequestMapping("/to_Login")
    public String toLogin() {
        return "login";
    }
    //登录验证
    @RequestMapping("/login")
    public String login(Member member, Model model, HttpSession httpSession) {
        System.out.println("收到数据");
        System.out.println(member);
        //安全验证
        if (member ==null||member.getNickname()==null
                ||member.getNickname().trim().equals("")
                ||member.getPassword()==null
                ||member.getPassword().trim().equals("")){
            model.addAttribute("msg","用户名或密码不能为空!");
            return "login";
        }
        Member member1 = memberService.getMemberByNicknameAndPwd(member);
        if (member1 != null) {
            httpSession.setAttribute("USER_SESSION",member1);
            model.addAttribute("name", member1.getName());
            return "main";
        }else {
            model.addAttribute("msg","用户名或密码错误!");
            return "login";
        }
    }
    //跳转到主页面
    @RequestMapping("/main")
    public String main(HttpSession httpSession,Model model) {
        Member user = (Member)httpSession.getAttribute("USER_SESSION");
        model.addAttribute("name", user.getName());
        return "main";
    }
    //跳转到首页
    @RequestMapping("/index")
    public String index() {
        return "index";
    }
    //会员列表页面
    @RequestMapping("/memberList")
    public String memberList(String nickname, Model model ,
                           @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        List<Member> memberList = memberService.getAllMembers(nickname, pageNum, pageSize);
        PageInfo<Object> pageInfo = new PageInfo<>(memberList);
        model.addAttribute("memberList",memberList);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("nickname",nickname);
        return "memberList";
    }
    //跳转到添加用户页面
    @RequestMapping("/addMember")
    @ResponseBody
    public Result addMember(Member member){
        Result<Object> result = new Result<>();
        if (member!=null
                && member.getNickname()!=null
                && !member.getNickname().trim().equals("")
                && member.getPassword()!=null
                && !member.getPassword().trim().equals("")
        ){
            System.out.println(member);
            //是否重名
            //SysUser user = sysUserService.getUserByUsername(sysUser.getUsername());
            Member member1 = memberService.getMemberByNickname(member.getNickname());
            if (member1!=null){
                result.setMsg("用户已经存在！");
                result.setSuccess(false);
                return result;
            }
            //int i = sysUserService.addUser(sysUser);
            int i = memberService.addMember(member);
            if (i>0){
                System.out.println("新增成功！");
                result.setMsg("新增成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("新增失败！");
                result.setMsg("新增失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入用户名和密码！");
            result.setMsg("请输入用户名和密码！");
            result.setSuccess(false);
            return result;
        }

    }
    //删除用户
    @RequestMapping("/delMember")
    @ResponseBody
    public Result delMember(@RequestParam("id") Integer id){
        Result<Object> result = new Result<>();
        if(id != null){
            //int i = sysUserService.deleteUser(id);
            int i = memberService.deleteMember(id);
            if (i > 0) {
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
    //批量删除
    @RequestMapping("/delAllMember")
    @ResponseBody
    public Result delAllMEmber(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);

        if (ids!=null){
            String[] strArray = ids.split(",");
            List<String> idsList = Arrays.asList(strArray);

            //int i = sysUserService.deleteAllUser(idsList);
            int i = memberService.deleteAllMember(idsList);
            if (i>0){
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择id！");
            result.setMsg("请选择id！");
            result.setSuccess(false);
            return result;
        }
    }
    //根据id查询用户
    @RequestMapping("/getMemberById")
    @ResponseBody
    public Result getMemberById(Integer id){
        Result<Object> result = new Result<>();
        System.out.println("收到查询数据");
        if(id != null){
            //SysUser user = sysUserService.getUserById(id);
            Member member = memberService.getMemberById(id);
            if (member != null){
                result.setData(member);
                System.out.println("查找成功！");
                result.setMsg("查找成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("没有此用户！");
                result.setMsg("没有此用户！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
    //编辑用户
    @RequestMapping("/editMember")
    @ResponseBody
    public Result editUser(Member member){
        Result<Object> result = new Result<>();
        if (member!=null
                && member.getNickname()!=null
                && !member.getNickname().trim().equals("")
                && member.getId()!=null
        ){
            System.out.println(member);
            //int i = sysUserService.editUser(sysUser);
            int i = memberService.editMember(member);
            if (i>0){
                System.out.println("修改成功！");
                result.setMsg("修改成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("修改失败！");
                result.setMsg("修改失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入用户名或id！");
            result.setMsg("请输入用户名或id！");
            result.setSuccess(false);
            return result;
        }

    }

    //用户详情
    @RequestMapping("/detailMember")
    @ResponseBody
    public Result detailMember(Integer id){
        Result<Object> result = new Result<>();
        System.out.println("收到查询数据");
        if(id != null){
            //SysUser sysUser = sysUserService.detailUser(id);
            Member member = memberService.detailMember(id);
            if (member!=null){
                result.setData(member);
                System.out.println("查找成功！");
                result.setMsg("查找成功！");
                result.setSuccess(true);
                return result;
            } else {
                System.out.println("没有此用户！");
                result.setMsg("没有此用户！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入id！");
            result.setMsg("请输入id！");
            result.setSuccess(false);
            return result;
        }
    }
}
